package com.boot.mall.cart.controller;

import com.boot.mall.cart.service.CartService;
import com.boot.mall.cart.vo.Cart;
import com.boot.mall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    // 获取购物车
    @GetMapping("/cart.html")
    public String getCartDetail(Model model) throws ExecutionException, InterruptedException {
        // 获取用户的购物车数据
        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
    }

    /**
     * 添加到购物车
     *      RedirectAttributes.addFlashAttribute()中添加的数据只会使用一次
     *      RedirectAttributes.addAttribute()中添加的数据会一直存在
     *      原理就是放到session当中
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/addCartItem")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        System.out.println(skuId + "----" + num);
        CartItem cartItem = cartService.addToCart(skuId,num);
        redirectAttributes.addFlashAttribute("cartItem",cartItem);
        return "redirect:http://cart.guli.com/success.html";
    }

    /**
     * 修改购物项的选中状态
     * @param isChecked
     * @param skuId
     * @return
     */
    @PostMapping("/checkCart")
    public String checkCart(@RequestParam("isChecked") Integer isChecked,@RequestParam("skuId") Long skuId){
        System.out.println(isChecked + "---" + skuId);
        cartService.checkCartItem(skuId,isChecked);
        return "redirect:http://cart.guli.com/cart.html";
    }

    /**
     * 修改购物项的数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,@RequestParam("num") Integer num){
        cartService.changeItemCount(skuId,num);
        return "redirect:http://cart.guli.com/cart.html";
    }

    /**
     * 删除购物项
     * @param skuId
     * @return
     */
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId){
        cartService.deleteItem(skuId);
        return "redirect:http://cart.guli.com/cart.html";
    }

    @GetMapping("/success.html")
    public String toSuccess(){
        return "success";
    }

    /**-----------------------------------远程接口---------------------------------**/

    /**
     * 查询选中的购物项
     * @param memberId
     * @return
     */
    @GetMapping("/getCheckItems/{memberId}/cartItems")
    @ResponseBody
    public List<CartItem> getCheckItems(@PathVariable("memberId") Long memberId){
        List<CartItem> items = cartService.getCheckItems(memberId);
        return items;
    }

}
